package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import dto.UserDTO;
import dto.UserQueryDTO;

import entity.Users;
import jakarta.annotation.Resource;

import org.example.constant.AccountConstant;
import org.example.constant.JwtClaimsConstant;
import org.example.repository.AccountRepository;
import org.example.repository.AdminRepository;
import org.example.result.PageResult;
import org.example.service.AdminService;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;


@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminRepository adminRepository;

    @Resource
    AccountRepository accountRepository;

//    更新管理员信息
    @Override
    public void updateInfo(UserDTO users) {
        switch (users.getDepartment()) {
            case AccountConstant.INSPECTOR_DEPARTMENT -> users.setRoleId(AccountConstant.INSPECTOR_ROLE_ID);
            case AccountConstant.PROCESS_ENGINEER_DEPARTMENT -> users.setRoleId(AccountConstant.PROCESS_ENGINEER_ROLE_ID);
            case AccountConstant.QUALITY_MANAGER_DEPARTMENT -> users.setRoleId(AccountConstant.QUALITY_MANAGER_ROLE_ID);
            case AccountConstant.SUPERVISOR_DEPARTMENT -> users.setRoleId(AccountConstant.SUPERVISOR_ROLE_ID);
        }
        accountRepository.updateInfo(users);
    }

//    增加员工信息
/*
 *    1.质检员
 *    2.工艺工程师
 *    3.质量经理
 *    4.供应链主管
 *    0.管理员
* */
    @Override
    public void addUser(UserDTO user) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(5);
        String encodedPassword = encoder.encode(user.getPassword());
        user.setPassword(encodedPassword);
        switch (user.getDepartment()) {
            case AccountConstant.INSPECTOR_NAME -> user.setRoleId(AccountConstant.INSPECTOR_ROLE_ID);
            case AccountConstant.PROCESS_ENGINEER_NAME -> user.setRoleId(AccountConstant.PROCESS_ENGINEER_ROLE_ID);
            case AccountConstant.QUALITY_MANAGER_NAME -> user.setRoleId(AccountConstant.QUALITY_MANAGER_ROLE_ID);
            case AccountConstant.SUPERVISOR_NAME -> user.setRoleId(AccountConstant.SUPERVISOR_ROLE_ID);
            case AccountConstant.ADMIN_NAME -> user.setRoleId(AccountConstant.ADMIN_ROLE_ID);
        }
        //user.setCreateTime(LocalDateTime.now());
        //user.setUpdateTime(LocalDateTime.now());
        adminRepository.addUser(user);
    }

    @Override
    @Transactional
    public PageResult queryUsers(UserQueryDTO userQueryDTO) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get(JwtClaimsConstant.USER_ID);
        userQueryDTO.setCurrentLoginUserId(userId);
        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());
        Page<Users> page = adminRepository.queryUsers(userQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //删除员工，but等待其他结构完成
    @Override
    public boolean deleteUser(Users users) {
        return false;
    }

//  重置密码
    @Override
    public void resetPassword(String username) {
        Users user = accountRepository.findByUsername(username);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(5);
        String encodedPassword = encoder.encode(AccountConstant.DEFAULT_PASSWORD);
        userDTO.setPassword(encodedPassword);
        accountRepository.updateInfo(userDTO);
    }

    @Override
    public boolean isUsernameExists(String username) {
        return adminRepository.existsByUsername(username);
    }

}